<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_Mobile_User extends Controller{
      //
	 public function action_login()
	 {

      $callback=$this->request->query("callback");

      $result=array();

		  $user_email=trim($this->request->post("login_user"));

      $user_password=sha1(trim($this->request->post("login_password")));

      if($user_email==""){


          $result['code']="0x00001";

          $result['msg']="The email field is required.";

          $result=json_encode($result);

      }
      else if($user_password==""){

        $result['code']="0x00001";

        $result['msg']="The password field is required.";
        
      }
      else{

          $model=new Model_Mobile_User();

          $result=$model->user_login($user_email,$user_password);
      }
      if(!empty($callback)&&$callback=="json_call"){

        exit($callback."($result)");

      }
      exit($result);

	 }
   public function action_register()
   {

      $callback=$this->request->query("callback");

      $user_name=$this->request->post("register_username");

      $user_email=$this->request->post("register_email");

      $password=$this->request->post("register_password");

      if($user_name==""){

        $result['code']="0x00001";

        $result['error_info']=array('username'=>'The username field is required.');

      }
      else if($user_email==""){

        $result['code']="0x00001";

        $result['error_info']=array('register_email'=>'The register_email field is required.');

        $result=json_encode($result);

      }
      else if($password==""){

        $result['code']="0x00001";

        $result['error_info']=array('password'=>'The password field is required.');

        $result=json_encode($result);

      }
      else{

          $model=new Model_Mobile_User();

          $result=$model->register($user_name,$user_email,$password);
      }

      if(!empty($callback)&&$callback=="json_call"){

        exit($callback."($result)");

      }
      exit($result);
      
   }
   public function action_profile(){

      $firstname=$this->request->post("firstname");

      if(empty($firstname)){

        echo json_encode(array(
          'msg'=>"firstname is required"
        ));

        exit();
      }

      $lastname=$this->request->post("lastname");

      if(empty($firstname)){

        echo json_encode(array(
          'msg'=>"lastname is required"
        ));

        exit();
      }

      $new_password=$this->request->post("new_password");

      $old_password=$this->request->post("old_password");

      $user=new Model_User();

      echo $user->saveProfile($firstname, $lastname,$new_password,$old_password);

   }

   public function action_reset(){

      $user_email=$this->request->post("email");

      if(empty($user_email)){

        echo  json_encode(array(
          'msg'=>"email is required"
        ));
        exit();

      }

      $total= DB::select(array(DB::expr('COUNT(1)'), 'total'))->from('site_user')->where("user_email",'=',$user_email)->execute()->get("total");

      if($total==0){

        echo  json_encode(array(
          'msg'=>"Sorry,the email does not exist"
        ));
        exit();
      }

      $token = md5(uniqid(rand())); 

      DB::insert('reset_info',array('user_email','token','createdon'))
      ->values(
        array($user_email,$token,time() 
      ))->execute();

      $url="http://dev.site.pt.com/user/reset?token=$token";

      $email_model=new Model_Email();

      $email_model->sendMail($user_email,'reset your password','You can <a href="'.$url.'" style="text-decoration:underline;color:#ff5a09">reset your password </a> now.');

      echo  json_encode(array(
        'code'=>"0x00000",
        'msg'=>'Thanks,We have send  an email to you,please check your email to reset your password.'
      ));
      exit();
   }

   public function action_resetPwd(){

      $token=$this->request->param('id');

      $query= DB::select(array(DB::expr('reset_info.user_email'), 'user_email'),'site_userid','fullname')
      ->from('reset_info')->where("reset_info.token",'=',$token)
      ->join('site_user','LEFT')->on('site_user.user_email' , '=' , 'reset_info.user_email' )
      ->execute();

      $user_email=$query->get("user_email");

      if(empty($user_email)){

        echo  json_encode(array(
          'msg'=>"Sorry,the token has expired!"
        ));
        exit();
      }

      $new_password=sha1($this->request->post("password"));

      $sql="update site_user
            set user_pwd='$new_password'
            where user_email='$user_email'";  

      $total_rows=DB::query(null,$sql)->execute();

      $sql="delete from reset_info
            where  token='$token'";

      DB::query(null,$sql)->execute();


      Session::instance()->set('username',$query->get("fullname"));

      Session::instance()->set('userid',$query->get("site_userid"));

      if($total_rows>0){

        echo  json_encode(array(
          'code'=>"0x00000",
          'msg'=>"Thanks,Change the password successfully."
        ));

        exit();
        
      }
      else{

        echo  json_encode(array(
          'code'=>"0x00000",
          'msg'=>"Sorry,try it again!"
        ));

        exit();

      }
   }
   public function action_wish(){

        $username=$this->request->param("id");

        if(empty($username)){

            if($this->request-> is_ajax()){

                $result=array();

                $result['code']='0x00001';

                exit(json_encode($result));
            }

            $this->redirect("/");

            exit();

        }

        $user_model=new Model_User();

        $user_info=$user_model->getInfoByUserName($username);

        if(empty($user_info)){

            $this->redirect("/");
            exit();
        }

        if($this->request-> is_ajax()){

            $page=$this->request->post("page");

            $pageSize=$this->request->post("pageSize");

        }
        else{

            $page=$this->request->query("page");

            $pageSize=$this->request->query("pageSize");

        }

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $wish_model=new Model_Wish();

        $listInfo=$wish_model->getInfoByUser($user_info['userid'],$offset,$pageSize);

        if($this->request-> is_ajax()){

            $result=array();

            if($page==1){

              $url="/u/".$username."/wish";

              $view=View::factory('mobile/user/wish');

              $view->url=$url;

              $view->user_info=$user_info;

              $view->wish_list=$listInfo['rows'];

              $view->pages=ceil($listInfo['total']/$pageSize);

              $view->total=$listInfo['total'];

              $result['data']=$view->render();

            }
           else{

               $result['data']=Block_User::wish_list($user_info,$wish_list);
           }
            exit(json_encode($result));

        }

        $url="/u/".$username."/wish";

        $view=View::factory('user/wish');

        $view->url=$url;

        $view->user_info=$user_info;

        $view->wish_list=$listInfo['rows'];

        $view->pages=ceil($listInfo['total']/$pageSize);

        $view->total=$listInfo['total'];

        $this->template->title = __($username."-Wish");

        $this->template->content =$view;
    }
    public function action_praise(){

        $username=$this->request->param("id");

        if(empty($username)){

            if($this->request-> is_ajax()){

                $result=array();

                $result['code']='0x00001';

                exit(json_encode($result));
            }

            $this->redirect("/");

            exit();

        }

        $user_model=new Model_User();

        $user_info=$user_model->getInfoByUserName($username);

        if(empty($user_info)){

            $this->redirect("/");
            exit();
        }

         $pageSize=10;

        if($this->request-> is_ajax()){

           $offset=intval($this->request->post("offset"))?intval($this->request->post("offset")):0;

        }
        else{

             $page=$page==""||$page==0?1:intval($page);

             $offset=$pageSize*($page-1);

        }

        $post_model=new Model_Post();

        $listInfo=$post_model->getPraiseByUser($user_info['userid'],$offset,$pageSize);

        $posts=$listInfo['list'];

        $total=$listInfo['total'];

        if($this->request-> is_ajax()){

          $result=array();

          if($offset==0){

               $url="/u/".$username."/praise";

                $view=View::factory('mobile/user/praise');

                $view->url=$url;

                $view->user_info=$user_info;

                $view->posts=$posts;

                $view->pages=ceil($total/$pageSize);

                $view->total=$total;

                $result['data']=$view->render();
        
          }
          else{

              $result['data']=Block_User::post_list($user_info,$posts);
          }

          exit(json_encode($result));

        }

        $url="/u/".$username."/praise";

        $view=View::factory('user/praise');

        $view->url=$url;

        $view->user_info=$user_info;

        $view->posts=$posts;

        $view->pages=ceil($total/$pageSize);

        $view->total=$total;

        $this->template->title = __($username."-Praise");

        $this->template->content =$view;
    }
    public function action_review(){

        $username=$this->request->param("id");

        if(empty($username)){

            if($this->request-> is_ajax()){

                $result=array();

                $result['code']='0x00001';

                exit(json_encode($result));
            }

            $this->redirect("/");

            exit();

        }

        $user_model=new Model_User();

        $user_info=$user_model->getInfoByUserName($username);

        if(empty($user_info)){

            $this->redirect("/");
            exit();
        }

        if($this->request-> is_ajax()){

            $page=$this->request->post("page");

            $pageSize=$this->request->post("pageSize");

        }
        else{

            $page=$this->request->query("page");

            $pageSize=$this->request->query("pageSize");

        }

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $post_model=new Model_Post();

        $listInfo=$post_model->getReviewByUser($user_info['userid'],$offset,$pageSize);

        $posts=$listInfo['list'];

        $total=$listInfo['total'];

        if($this->request-> is_ajax()){

            $url="/u/".$username."/review";

            $view=View::factory('mobile/user/review');

            $view->url=$url;

            $view->user_info=$user_info;

            $view->post_list=$posts;

            $view->pages=ceil($total/$pageSize);

            $view->total=$total;

            $result=array();

            $result['data']=$view->render();

            exit(json_encode($result));

        }

        $url="/u/".$username."/review";

        $view=View::factory('user/review');

        $view->url=$url;

        $view->user_info=$user_info;

        $view->posts=$posts;

        $view->pages=ceil($total/$pageSize);

        $view->total=$total;

        $this->template->title = __($username."-Review");

        $this->template->content =$view;
    }
    
    public function action_setting(){

        $user=Session::instance()->get('user');

        if(empty($user)){

            $this->redirect("/");

            exit();

        }

        $model_user=new Model_User();

        $userInfo=$model_user->getInfoByUserId($user['userid']);

        $view=View::factory('user/setting');

        $view->userInfo=$userInfo;

        if($this->request-> is_ajax()){

            $result=array();

            $result['data']=$view->render();

            exit(json_encode($result));

        }


        $this->template->content =$view;

    }
 } 
